<template>
  <div class="book-detail">
    <div class="book-item">
      <img v-lazy="book.cover"/>
      <div class="content">
        <div class="title">{{book.name}}</div>
        <div class="author">{{book.author}}</div>
        <div class="gold">可获得金币{{book.gold_coin}}个，已获得金币{{book.get_gold_coin}}个</div>
      </div>
    </div>
    <section class="section" v-for="(capter,index) in capters" :key="capter.id">
      <router-link replace tag="div" :to="{path:'/chapter',query:{id:capter.id}}" v-if="index <= chapter_index">
        <div>{{capter.title}}</div>
        <div class="count">可获得金币{{capter.gold_coin || 0}}个，已获得金币{{capter.get_gold_coin}}个</div>
        <i class="icon-right"/></router-link>
      <div v-else>
        <div>{{capter.title}}</div>
        <div class="count">可获得金币{{capter.gold_coin || 0}}个，已获得金币{{capter.get_gold_coin}}个</div>
        <i class="icon-lock"/>
      </div>
    </section>
    <ul class="tab-bar">
      <div class="sent" @click.stop="goBack">
        <i class="icon-sent"/>
      </div>
      <li class="index">
        <i class="icon-price"/> <countup :start-val="0" :end-val="student.gold_coin" :duration="2"></countup>
      </li>
      <li class="index">
        <i class="icon-count"/> <countup :start-val="0" :end-val="student.word_count" :duration="2"></countup>
      </li>
    </ul>
    <!-- <i class="icon-back" @click.stop="goBack"/> -->
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { Countup } from "vux";

export default {
  components: { Countup },
  data() {
    return {
      book: {},
      capters: [],
      chapter_index: 0 //根据章节数判断哪些章节可被解锁
    };
  },
  props: {},
  watch: {},
  methods: {
    goBack() {
      this.$router.replace("/find");
    },
    addArticle() {
      this.$$api_book_save_student_book({
        data: {
          user_id: this.userID,
          student_id: this.student.id,
          book_id: this.book.id,
          describe: this.book.describe,
          cover: this.book.cover,
          price: this.book.price,
          chapter_count: this.book.chapter_count,
          author: this.book.author,
          word_count: this.book.word_count,
          name: this.book.name,
          read_chapter_count: 0,
          read_word_count: 0,
          read_state: "reading",
          gold_coin: this.book.gold_coin,
          get_gold_coin: 0,
          chapter_index: 0 //章节位置默认是0
        }
      });
    }
  },
  filters: {},
  computed: {
    ...mapGetters(["userID", "student"])
  },
  created() {
    document.title = "书籍详情";
  },
  mounted() {
    this.$$api_book_chapterList({
      data: this.$router.currentRoute.query.id,
      fn: res => {
        console.log("res:::", res);
        this.capters = res;
        //查询当前学生阅读的章节获得的金币数
        this.$$api_book_student_chapter_gold({
          data: {
            student_id: this.student.id,
            book_id: this.$router.currentRoute.query.id
          },
          fn: res => {
            this.capters = this.capters.map(item => {
              const capter = res.filter(
                capter_ => capter_.chapter_id == item.id
              );
              return !!capter.length
                ? { ...item, get_gold_coin: capter[0].get_gold_coin }
                : { ...item, get_gold_coin: 0 };
            });
          }
        });
      }
    });
    //进入到书籍详情时判断是否要将书籍加入到书单，章节读完后只需要更新完成章节数
    //因为添加书单需要获取书籍详情，所以这里我们通过异步的方式先获取到书籍内容再判断是否要进行书单的添加
    new Promise(resolve => {
      this.$$api_book_findArticle({
        data: this.$router.currentRoute.query.id,
        fn: res => {
          this.book = res;
          resolve(res);
        }
      });
    }).then(book => {
      this.$$api_book_check_student_book_status({
        data: {
          book_id: this.$router.currentRoute.query.id,
          student_id: this.student.id
        },
        fn: res => {
          this.book = { ...this.book, get_gold_coin: res.get_gold_coin };
          //检查该书籍是否在书单中，不在的话将书籍加入到我们自己的书单中
          if (!res.state) {
            this.addArticle();
          }
          //获取章节数
          this.chapter_index = res.chapter_index || 0;
        }
      });
    });
  },
  destroyed() {
    document.title = "课外读物";
  }
};
</script>

<style lang="scss" scoped>
@import "~@/assets/scss/const.scss";
@import "~@/assets/scss/mixin.scss";

.book-detail {
  background: $app-background-color;
  padding-bottom: 50px;
  .book-item {
    background: $app-background-color;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    margin: 20px;
    margin-bottom: 10px;
    width: 90%;
    overflow: hidden;
    justify-content: center;
    align-items: center;
    img {
      display: block;
      width: 90px;
      min-width: 90px;
      height: 120px;
      margin-right: 10px;
      background: #e9e9e9;
    }
    .content {
      flex: 1;
      line-height: 20px;
      font-size: 12px;
      color: #999999;
      font-family: PingFang-SC-Medium;
      line-height: 22px;
      min-width: 0;
      .title {
        font-size: 16px;
        color: #1a1a1a;
        height: 44px;
        @include ellipsis(2);
      }
      .gold {
        font-size: 11px;
        color: #999999;
        margin-bottom: 8px;
        @include ellipsis(1);
      }
    }
  }
  .section {
    height: 60px;
    border-bottom: 1px solid lightgray;
    padding-left: 20px;
    padding-top: 10px;
    > div {
      position: relative;
      > div {
        height: 20px;
        line-height: 20px;
        color: #000000;
        font-size: 13px;
        &.count {
          font-size: 12px;
          color: rgba(85, 85, 85, 0.3);
        }
      }
      i {
        position: absolute;
        top: -10px;
        right: 0;
        height: 60px;
        line-height: 60px;
        width: 30px;
      }
    }
  }

  .tab-bar {
    @include onepx();
    position: fixed;
    z-index: 100;
    bottom: 0;
    width: 100%;
    height: 50px;
    text-align: center;
    display: flex;
    flex-direction: row;
    background-color: #ffffff;
    // box-shadow: 0px 1px 10px 1px #888;
    .sent {
      width: 48px;
      font-size: 30px;
      color: #65acdf;
      line-height: 42px;
      border-right: 1px solid rgba(7, 17, 27, 0.1);
    }
    li {
      flex: 1;
      // color: #999;
      display: block;
      color: #65acdf;
      line-height: 50px;
      i {
        font-size: 18px;
      }
      &:first-child {
        border-right: 1px solid rgba(7, 17, 27, 0.1);
      }
    }

    li.router-link-active {
      color: #018cfc;
      transform: scale(1.1);
    }
  }
  .icon-back {
    position: fixed;
    top: 10px;
    left: 10px;
    font-size: 40px;
    color: #999999;
  }
}
</style>

